<template>
	<view class="container" :style="[{paddingTop:CustomBar+'px'}]">
		<cu-custom bgColor="bg-colorP" :isBack="true">
			<block slot="backText"></block>
			<block slot="content">搜索商家</block>
		</cu-custom>
		<view class="search-content">
			<view class="header-s">
				<view class="search">
					<image class="icon" src="http://xarkbucket.oss-cn-hangzhou.aliyuncs.com/search.svg"></image>
					<input type="text" confirm-type="搜索" @confirm="search" placeholder='请输入商户或景点名称...' placeholder-class="placeholder"
					 v-model="searchVal"></input>
				</view>
				<view class="search-btn" @click='search'>搜索</view>
			</view>

			<view class="history-info" v-if="newActivityList.length>0">
				<view class="title">热门搜索</view>
				<view class="his-con">
					<view v-for="(item,index) in newActivityList" class='history-item' :key="index" :data-text="item.name" @click='historySearch'>{{item.name}}</view>
				</view>
			</view>

			<view class="history-info" v-if="historyList.length>0">
				<view class="title">历史搜索</view>
				<view class="his-con">
					<view v-for="(item,index) in historyList" class='history-item' :key="index" :data-text="item" @click='historySearch'>{{item}}</view>
				</view>
			</view>

			<view class="history-info">
				<view class="title">搜索结果</view>
				<shop-item v-for="(item,index) in list_data" :key="index" :item="item" v-if="hasContent"></shop-item>
				<no-content :showType="'search'" v-if='noContentShow'></no-content>
			</view>
		</view>
	</view>
</template>

<script>
	import noContent from './component/noContent.vue'
	import shopItem from './component/shopItem.vue'
	export default {
		components: {
			noContent,
			shopItem
		},
		data() {
			return {
				CustomBar: this.CustomBar,
				noContentShow: false,
				historyList: [],
				page: 1,
				searchVal: '',
				newActivityList: [],
				num: '',
				hasContent: false,
				list_data: []
			}
		},
		// watch:{
		// 	me.hasContent = true
		// },
		onLoad() {
			this.getShops()
		},
		onShow() {
			if (uni.getStorageSync('historyList')) {
				var list = uni.getStorageSync('historyList')
				if (list.length > 5) {
					list.length = 5
					uni.setStorageSync('historyList', list)
				}
				this.historyList = list
			}
		},
		methods: {
			getShops() {
				var me = this
				this.$request({
					url: '/shop/getRecommendShopOrder',
					data: {
						latitude: uni.getStorageSync('coordinate').latitude,
						longitude: uni.getStorageSync('coordinate').longitude
					},
					success: res => {
						if (res.data.status == 1) {
							if (res.data.data.pageCount == 0) return
							res.data.data.data = res.data.data.data.splice(0, 10)
							me.newActivityList = res.data.data.data
						}
						uni.hideLoading()
					},
					fail: () => {},
					complete: () => {}
				});
			},
			search() {
				var me = this
				me.noContentShow = false
				me.page = 1
				this.$request({
					url: '/shop/nearbyBusinessNew',
					data: {
						latitude: uni.getStorageSync('coordinate').latitude,
						longitude: uni.getStorageSync('coordinate').longitude,
						search_info: me.searchVal,
						page: me.page
					},
					success: res => {
						if (res.data.status == 1) {
							if (res.data.data.shops.length == 0) {
								me.list_data = []
								me.noContentShow = true
								return false
							}
							me.list_data = res.data.data.shops
							me.num = res.data.data.totalNum
							me.hasContent = true
							me.page++
							var historyList = me.historyList.length > 10 ? me.historyList.splice(0, 9) : me.historyList;
							var filterItem = me.historyList.filter(item => item == me.searchVal);
							if (filterItem.length > 0) {

							} else {
								historyList.unshift(me.searchVal);
								me.historyList = historyList
							}
							uni.setStorageSync('historyList', historyList)
						}
						uni.hideLoading()
					},
					fail: () => {},
					complete: () => {}
				});
			},
			historySearch(e) {
				var text = e.currentTarget.dataset.text;
				if (text) {
					this.searchVal = text
					this.page = 1
					this.search()
				}
			},
		}
	}
</script>

<style>
	page {
		background: #fff;
	}
</style>
<style scoped>
	.header-s {
		display: flex;
		box-sizing: border-box;
		align-items: center;
		height: 100rpx;
		width: 100%;
		padding: 0 20rpx;
		background: #fff;
		z-index: 999
	}

	.search {
		flex: 1;
		height: 60rpx;
		background: #f3f3f3;
		display: flex;
		align-items: center;
		box-sizing: border-box;
		border-radius: 30rpx;
		padding: 0 20rpx;
		margin-right: 20rpx;
	}

	.placeholder {
		color: #999;
		font-size: 26rpx;
	}

	.search .icon {
		width: 35rpx;
		height: 40rpx;
	}

	.search input {
		flex: 1;
		box-sizing: border-box;
		padding-left: 20rpx;
		font-size: 28rpx;
		color: #999;
	}

	.search-btn {
		font-size: 26rpx;
		color: #3080E8
	}

	.history-info {
		width: 100%;
		height: auto;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.history-info .title {
		height: 88rpx;
		line-height: 88rpx;
		color: #999999;
		font-size: 30rpx;
	}

	.history-info .his-con {
		display: flex;
		flex-wrap: wrap;
	}

	.history-item {
		padding: 10rpx 15rpx;
		margin-right: 20rpx;
		background: #F4F4F4;
		border-radius: 8rpx;
		color: #333;
		font-size: 26rpx;
		margin-bottom: 10rpx;
	}



	.noLength {
		color: #999;
		font-size: 26rpx;
	}


	.news-alive {
		width: 100%;
		overflow: hidden;
		display: flex;
		padding-bottom: 20rpx;
		box-sizing: border-box;
		position: relative;
		border-bottom: solid 1rpx #f2f2f2;
	}

	.news-alive .head-img {
		width: 150rpx !important;
		height: 150rpx !important;
		border-radius: 8rpx;
		margin-right: 10rpx;
		display: block;
	}

	.news-alive>view:nth-child(2) {
		flex: 1;
	}

	.news-alive>view:nth-child(2)>view:nth-child(1) {
		height: 50rpx;
		display: flex;
	}

	.news-alive>view:nth-child(2)>view:nth-child(1) text {
		font-size: 30rpx;
		color: #333;
		font-weight: bold;
	}

	.news-alive>view:nth-child(2)>view:nth-child(1) image {
		width: 70rpx;
		height: 45rpx;
		margin-left: -10rpx;
	}

	.news-alive>view:nth-child(2)>view:nth-child(2) {
		color: #999;
		font-size: 26rpx;
		margin-top: 10rpx;
	}

	.news-alive>view:nth-child(2)>view:nth-child(2) text {
		color: rgba(246, 96, 132, 1);
	}

	.news-alive>view:nth-child(2)>view:nth-child(3) {
		font-size: 24rpx;
		margin-top: 10rpx;
		color: #666;
	}

	.news-alive>view:nth-child(2)>view:nth-child(3) text {
		color: rgba(246, 96, 132, 1);
	}

	.news-alive .position {
		width: 160rpx !important;
		height: 50rpx;
		line-height: 50rpx;
		position: absolute;
		right: 20rpx;
		top: 50%;
		transform: translateY(-25rpx);
		color: rgba(255, 255, 255, 1);
		background-color: #3080E8;
		border-radius: 30rpx;
		font-size: 24rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.news-alive .position image {
		width: 26rpx;
		height: 26rpx;
		display: inline-block;
		margin-right: 5rpx;
	}
</style>
